<template>
  <div class="app-container">
    <div class="filter-container">
      <el-row>
        <!-- 咨询 日期 -->
        <el-date-picker
          v-model="listQuery.queryStartDate"
          align="right"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="试听开始日期"
          style="width: 180px"
        ></el-date-picker>
        <el-date-picker
          v-model="listQuery.queryEndDate"
          style="width: 180px"
          align="right"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="试听结束日期"
        ></el-date-picker>
        <el-button
          v-waves
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >{{ $t("table.search") }}</el-button>
        <el-button
          v-waves
          :loading="downloadLoading"
          v-permission="['business:export']"
          class="filter-item"
          type="primary"
          icon="el-icon-download"
          @click="handleDownload"
        >{{ $t('table.export') }}</el-button>
      </el-row>
    </div>
<el-alert

						  title=""
						  type="info"
						  description="温馨提示:默认显示本月数据"
						  >
						</el-alert>
    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >

      <el-table-column label="老师姓名" align="center" >
        <template slot-scope="scope">
          <span>{{ scope.row.statTeacherName }}</span>
        </template>
      </el-table-column>
			<el-table-column label="校区" align="center" >
			  <template slot-scope="scope">
			    <span>{{ scope.row.statOrganizationName }}</span>
			  </template>
			</el-table-column>
      <el-table-column label="试听课次数" width align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.statTryNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="成单数" width align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.statEnrollNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="转化率" width align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.statConversionRate }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getList"
    />
  </div>
</template>

<script>
  import { fetchOrg, fetchChannel, fetchCourse } from "@/api/clue"
  import {
    fetchTeacherEnrollRateCon,// 渠道信息,
    fetchExportTeacherEnrollRateCon
  } from "@/api/statistics"
  import waves from "@/directive/waves" //  水波纹指令
  import Pagination from "@/components/Pagination" //  Secondary package based on el-pagination
  import { parseTime } from "@/utils";
import permission from "@/directive/permission/index.js"; // 权限判断指令

  export default {
    name: "campusVisitCon",
    components: { Pagination },
    directives: {
      waves,
    permission
    },
    data() {
      return {
        orgIdOptions: [], // 校区
        channelTypeIdOptions: [], // 渠道类型
        channelIdOptions: [], // 渠道
        departmentIdOptions: [], // 科系
        courseOptions: [], // 课程
        ownerIdOptions: [], // 咨询师
        tableKey: 0,
        list: null,
        total: 0,
        listLoading: true,
        listQuery: {
          current: 1,
          size: 20,
          queryStartDate: "",
          queryEndDate: "",
        },
        downloadLoading: false,
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        fetchTeacherEnrollRateCon(this.listQuery).then(response => {
          this.list = response.data
          this.total = response.count
          this.listLoading = false
        })
      },
      handleFilter() {
        this.listQuery.current = 1
        this.getList()
      },
      handleDownload() {
        this.downloadLoading = true;
        fetchExportTeacherEnrollRateCon(this.listQuery).then(response => {
          const exportList = response.data;
          import("@/vendor/Export2Excel").then(excel => {
            const tHeader = [
              "老师姓名",
							"校区",
              "试听课次数",
              "成单数",
              "转化率"
            ];
            const filterVal = [
              "statTeacherName",
							"statOrganizationName",
              "statTryNum",
              "statEnrollNum",
              "statConversionRate"
            ];
            const data = this.formatJson(filterVal, exportList);
            excel.export_json_to_excel({
              header: tHeader,
              data,
              filename: "老师试听报名率"
            });
            this.downloadLoading = false;
          });
        });



      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v =>
          filterVal.map(j => {
            if (j === "createTime") {
              return parseTime(v[j]);
            } else {
              return v[j];
            }
          })
        );
      }
    }
  }
</script>

